package com.ml.admin.controller;

import com.ml.admin.pojo.vo.UserVO;
import com.ml.admin.service.LoginService;
import com.ml.admin.utils.ResponseEnum;
import com.ml.admin.utils.ReturnResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author ml
 * @date 2023/2/24 13:55
 */
@RestController
@RequestMapping("/login")
public class LoginCotroller {
    @Autowired
    private LoginService loginService;

    /**
     * 手机号注册
     * @param vo 手机号，密码等参数
     * @return 注册成结果
     */
    @PostMapping("/register")
    public ReturnResponse register(@RequestBody UserVO vo){
        try{
            return loginService.register(vo);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }
    }

    /**
     * 登录 1:手机验证码登录，如果不存在即注册
     *       2：密码登录，需用户设置密码
     * @param vo 手机号，密码等参数
     * @return 登录结果
     */
    @PostMapping("/login")
    public ReturnResponse login(@RequestBody UserVO vo){
        try{
            return loginService.login(vo);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }
    }
    /**
     * 获取邮箱验证码
     * @param email,type 邮箱, 类型（分注册时发送验证码 1和 忘记密码时发送验证码 2）
     * @return 成功 or 失败
     */
    @GetMapping("/getmailcode")
    public ReturnResponse getMailCode(String email,String type) {
        try{
            return loginService.getMailCode(email,type);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }

    }
    /**
     * 验证码校验
     * @param vo 验证码 邮箱
     * @return 成功or失败
     */
    @PostMapping ("/checkuser")
    public ReturnResponse checkUser(@RequestBody UserVO vo){
        try{
            return loginService.checkUser(vo);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnResponse.failure(ResponseEnum.FAILURE);
        }
    }
}
